package com.hfut.jpoa.service.impl;

import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.PinYin4jUtil;
import com.hfut.jpoa.dao.UserDAO;
import com.hfut.jpoa.dto.UserDTO;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;

public class UserServiceImpl implements UserService
{
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(UserServiceImpl.class);
	private UserDAO userDAO;

	public Integer saveUser(User user) throws UserException
	{
		if (null == user)
		{
			LOGGER.error("user is empty.");
			throw new UserException("user info is not complete");
		}
		return userDAO.insert(user);

	}

	public int deleteUser(String loginId) throws UserException
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public User findUser(String loginId) throws UserException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public User findUser(String username, String password) throws UserException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public void setUserDAO(UserDAO userDAO)
	{
		this.userDAO = userDAO;
	}

	public ResultDto<User> list(UserDTO userDTO) throws UserException
	{
		return userDAO.findUserInfoForPage(userDTO);
	}

	public ResultDto<User> queryUserInfoForPage(UserDTO userDTO)
	        throws UserException
	{
		return userDAO.findUserInfoForPage(userDTO);
	}

	public String createUserCode(User user) throws UserException
	{
		if (null == user && StringUtils.isBlank(user.getName()))
		{
			LOGGER.error("createUserCode: user is empty.");
			throw new UserException("createUserCode: user info is not complete");
		}
		// 根据用户名自动生成首字母大写
		return PinYin4jUtil.spell(user.getName());
	}

	public boolean codeValidator(User user) throws UserException
	{
		if (null == user && StringUtils.isBlank(user.getUserCode()))
		{
			LOGGER.error("codeValidator异常：值为空 ");
			throw new UserException("codeValidator异常：值为空 ");
		}
		List<User> lstUser = userDAO.queryUserList(user);
		if (lstUser != null && lstUser.size() > 0)
		{
			return true;
		}
		return false;
	}

	public User findUserInfo(User record) throws UserException
	{
		if (null == record && StringUtils.isBlank(record.getUserCode()))
		{
			LOGGER.error("findUserInfo异常：值为空 ");
			throw new UserException("findUserInfo异常：值为空 ");
		}
		User result = userDAO.selectByPrimaryKey(record.getUserCode());

		return result;
	}

	public void updateUser(User record) throws UserException
	{
		if (null == record && StringUtils.isBlank(record.getUserCode()))
		{
			LOGGER.error("updateUser异常：值为空 ");
			throw new UserException("updateUser异常：值为空 ");
		}
		userDAO.updateByPrimaryKeySelective(record);

	}

	public List<User> queryUser(User user) throws UserException
	{
		if (null == user)
		{
			LOGGER.error("findUserInfo异常：值为空 ");
			throw new UserException("findUserInfo异常：值为空 ");
		}
		return userDAO.queryUserList( user);
	}
}
